/**
 * 
 */
package com.manolo.client.connection;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.gson.Gson;
import com.manolo.client.entity.*;
import com.manolo.client.dispatcher.ReciveDateDispatcher;
import com.manolo.client.event.DateEvent;

/**
 * @author gustavo
 * 
 */
public class ClientPlayerListener implements Runnable {

	private Integer playerId;

	private BufferedReader in;
	private Socket socket;

	private Player player;

	public ClientPlayerListener(Socket socket, Player player) {

		this.socket = socket;
		this.player = player;

		try {
			in = new BufferedReader(new InputStreamReader(
					socket.getInputStream()));

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void receive() throws IOException {
		String msg = in.readLine();
		if (msg != null && msg.trim().length() > 1) {
			Gson gson = new Gson();
			Player obj2 = gson.fromJson(msg, Player.class);
			System.out.println(msg);
		
			
			DateEvent event = new DateEvent();
			event.setId(obj2.getId());
			event.setName(obj2.getName());
			event.getCards().clear();
			event.getCards().addAll(obj2.getCards());
			
			ReciveDateDispatcher.getInstance().dispatchDateEvent(event);
			System.out.println("Sistem "+player);
			
//			if (player != null && player.getId() == obj2.getId()) {
//				if (obj2.getCards().size() > 0) {
//					player.getCards().clear();
//					player.getCards().addAll(obj2.getCards());
//					
//				}
//
//			}
		}
	}

	@Override
	public void run() {
		while (true) {
			try {
				receive();

			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}